package cn.aishua.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.util.Log;

public class FileCache
{
    //    private Context context;

    private File mCacheDir;
    private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M
    private final LruCache<String, Long> sFileCache = new LruCache<String, Long>( MAX_CACHE_SIZE )
    {
        @Override
        public int sizeOf( String key, Long value )
        {
            return value.intValue();
        }

        @Override
        protected void entryRemoved( boolean evicted, String key, Long oldValue, Long newValue )
        {
            File file = getFile( key );
            if( file != null )
                file.delete();
        }
    };

    public FileCache(Context context)
    {
        //        this.context = context;
        mCacheDir = context.getCacheDir();
        Log.d( "file cache", "cache dir: " + mCacheDir.getAbsolutePath() );
    }

    private File getFile( String fileName )
    {
        File file = new File( mCacheDir, fileName );
        if( !file.exists() || !file.isFile() )
            file = null;
        return file;
    }

    //缓存bitmap到外部存储
    public boolean putBitmap( String key, Bitmap bitmap )
    {
        File file = getFile( key );
        if( file != null )
        {
            Log.d( "file cache", "file already exists" );
            return true;
        }
        FileOutputStream fos = getOutputStream( key );
        boolean saved = bitmap.compress( CompressFormat.JPEG, 100, fos );
        try
        {
            fos.flush();
            fos.close();
        }
        catch( IOException e )
        {
            Log.e( "file cache", "IOException" );
        }

        if( saved )
        {
            synchronized( sFileCache )
            {
                Log.d( "file cache", "put bitmap into file cache" );
                sFileCache.put( key, getFile( key ).length() );
            }
            return true;
        }
        return false;
    }

    //根据key获取OutputStream
    private FileOutputStream getOutputStream( String key )
    {
        if( mCacheDir == null )
            return null;
        FileOutputStream fos = null;
        ;
        try
        {
            fos = new FileOutputStream( mCacheDir.getAbsolutePath() + File.separator + key );
        }
        catch( FileNotFoundException e )
        {
            Log.e( "file cache", "FileNotFoundException" );
        }
        return fos;
    }

    //获取bitmap
    private static BitmapFactory.Options sBitmapOptions;
    static
    {
        sBitmapOptions = new BitmapFactory.Options();
        sBitmapOptions.inPurgeable = true; //bitmap can be purged to disk
    }

    public Bitmap getBitmap( String key )
    {
        Bitmap bitmap = null;
        File bitmapFile = getFile( key );
        if( bitmapFile != null )
        {

            try
            {
                bitmap = BitmapFactory.decodeStream( new FileInputStream( bitmapFile ), null, sBitmapOptions );
            }
            catch( FileNotFoundException e )
            {
                Log.e( "file cache", "FileNotFoundException" );
            }
            catch( OutOfMemoryError e )
            {
                Log.e( "file cache", "OutOfMemoryError" );
                System.gc();
            }
            if( bitmap != null )
            {
                MemoryCache.getInstance().putBitmap( key, bitmap );
                bitmap = null;
            }
        }
        return MemoryCache.getInstance().getBitmap( key );
    }
}
